package com.db.user;

import java.util.*;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class UserDAO {
	private SqlMapClientTemplate template;
	
	public void setTemplate(SqlMapClientTemplate template) {
		this.template = template;
	}

	public int getSabun() {
		int sabun = 0;
		try {
			Integer it = (Integer) template.queryForObject("getSabun");
			sabun = it.intValue();
		} catch (Exception e) {
			System.out.println("UserDAO.getSabun():" + e.toString());
		}
		return sabun;
	}

	public void insertUser(UserVO vo) {
		try {
			System.out.println(vo.toString());
			template.insert("insertUser", vo);
		} catch (Exception e) {
			System.out.println("UserDAO.insertUser():" + e.toString());
		}
	}
	
	public void updateUser(UserVO vo){
		try {
			template.update("updateUser", vo);
		} catch (Exception e) {
			System.out.println("UserDAO.updateUser():"+e.toString());
		}
	}

	public String verifyUser(UserVO vo) {
		String result = "";
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("sabun", new Integer(vo.getSabun()));
		userMap.put("pwd", vo.getPwd());
		
		int count = 0;
		try {
			count = (Integer) template.queryForObject("verifySabun", userMap);
			if (count == 0) {
				result = "nosabun";
			} else {
				count = (Integer) template.queryForObject("verifyPwd", userMap);
				if (count == 0) {
					result = "nopwd";
				} else {
					result = "login";
				}
			}
		} catch (Exception e) {
			System.out.println("UserDAO.verifyUser():" + e.toString());
		}
		return result;
	}
	
	public UserVO getUserInfo(int sabun){
		UserVO vo = null;
		try {
			vo = (UserVO) template.queryForObject("getUserInfo", sabun);
		} catch (Exception e) {
			System.out.println("UserDAO.getUserInfo():" + e.toString());
		}
		return vo;
	}
	
	public List<UserVO> getSuperiorUser(UserVO uvo){
		List<UserVO> list = new ArrayList<UserVO>();
		try {
			list = template.queryForList("getSuperiorUser", uvo);
		} catch (Exception e) {
			System.out.println("UserDAO.getSuperiorUser():" + e.toString());
		}
		return list;
	}
}
